﻿using System.Web.Mvc;
using Utils.Cache;
using Utils.Types;

namespace CMS.Areas.EasyUI.Controllers
{
    /// <summary>
    /// Base
    /// </summary>
    public class BaseController : Controller
    {
        private Model.Admin.UserInfo _baseuserinfo;

        /// <summary>
        /// 缓存接口
        /// </summary>
        protected CacheManager Cache => new CacheManager();

        /// <summary>
        /// 用户信息
        /// </summary>
        protected Model.Admin.UserInfo BaseUserInfo
        {
            set
            {
                _baseuserinfo = value;
                Cache.SetCache("UserInfo", _baseuserinfo, CacheEnum.Redis);
            }
            get
            {
                _baseuserinfo = Cache.GetCache<Model.Admin.UserInfo>("UserInfo");
                return _baseuserinfo;
            }
        }

        /// <summary>
        /// UserID
        /// </summary>
        protected int UserID => (int)BaseUserInfo.UserID;

        /// <summary>
        ///  执行控制器方法之前 
        /// </summary>
        /// <param name="context"></param>
        protected override void OnActionExecuting(ActionExecutingContext context)
        {
            Utils.Log.Log.Txt(BaseUserInfo);

            if (Request.UrlPath().EqualsIgnore("/EasyUI/Admin/Register") ||
                Request.UrlPath().EqualsIgnore("/EasyUI/Admin/Error") ||
                Request.UrlPath().EqualsIgnore("/EasyUI/Admin/Login") ||
                Request.UrlPath().EqualsIgnore("/EasyUI/Admin/Validate") ||
                Request.UrlPath().EqualsIgnore("/EasyUI/Admin/CheckLogin") ||
                Request.UrlPath().EqualsIgnore("/EasyUI/Admin/GetAreaList") ||
                Request.UrlPath().EqualsIgnore("/EasyUI/Admin/RegisterCheck") ||
                Request.UrlPath().EqualsIgnore("/EasyUI/UserInfo/GetUserList") ||
                Request.UrlPath().EqualsIgnore("/EasyUI/UserInfo/ManagerUser") ||
                Request.UrlPath().EqualsIgnore("/EasyUI/UserInfo/OperateUser") ||
                BaseUserInfo != null) return;
            Cache.SetCache("ReturnUrl", Request.Url, CacheEnum.Session);
            context.Result = Redirect("/EasyUI/Admin/Login");
        }
    }
}